package com.xuecheng.media.config;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.spring.util.ObjectUtils;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.vod.model.v20170321.*;
import com.google.gson.Gson;
import com.xuecheng.media.domain.AliyunProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

/**
 * @Author 12441
 * @Date 2022/9/18 15:50
 * @Version 1.0
 */
@Component
public class VodTemplate {
    @Autowired
    private AliyunProperties aliyunProperties;



    private DefaultAcsClient client;


    @PostConstruct
    //初始化连接
    private void init() {
        DefaultProfile profile = DefaultProfile.getProfile(
                aliyunProperties.getRegion(),
                aliyunProperties.getAccessKeyId(),
                aliyunProperties.getAccessKeySecret()
        );
         client = new DefaultAcsClient(profile);
    }


    /**
     * 获取上传凭证和地址
     * @param fileTitle 视频标题-不带拓展名
     * @param fileName 文件名
     * @return CreateUploadVideoResponse-获取视频上传地址和凭证响应数据
     * @throws ClientException
     */
    public CreateUploadVideoResponse CreateUploadVideo(String fileTitle,String fileName) throws ClientException {
        CreateUploadVideoRequest request = new CreateUploadVideoRequest();
        request.setTitle(fileTitle);
        request.setFileName(fileName);
        return client.getAcsResponse(request);


    }
    /**
     * 获取上传凭证和地址
     * @param videoId 视频ID
     * @return RefreshUploadVideoResponse-刷新视频凭证的响应数据
     * @throws ClientException
     */
    public RefreshUploadVideoResponse RefreshUploadVideo(String videoId) throws ClientException {

        RefreshUploadVideoRequest request = new RefreshUploadVideoRequest();
        request.setVideoId(videoId);

        return  client.getAcsResponse(request);
    }

    /**
     *  获得媒资播放请求对象
     *
     * @param videoId 视频ID
     * @return GetPlayInfoResponse-获取视频的播放信息
     * @throws ClientException
     */
    public GetPlayInfoResponse getPlayInfo(String videoId) throws ClientException {

        GetPlayInfoRequest request = new GetPlayInfoRequest();
        request.setVideoId(videoId);
        return  client.getAcsResponse(request);
}
    /**
     *  删除视频
     *
     * @param videoIds 视频ID
     * @throws ClientException
     */
    public void deleteVideo(String... videoIds) throws ClientException {
        DeleteVideoRequest request = new DeleteVideoRequest();
        if (ObjectUtil.isEmpty(videoIds)) {
            return ;
        }
        //支持传入多个视频ID，逗号隔开
        String videoIdsStr = String.join(",", videoIds);
        request.setVideoIds(videoIdsStr);
        client.getAcsResponse(request);
        return  ;
}



}
